using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITS.Core.Domain.News;
using System.Threading;
using ITS.DAL.News;

namespace ITS.DAL.Concrete.EF.News
{
    public class EFNewsRepository : INewsRepository
    {
        private readonly EnterpriseContext _context = new EnterpriseContext();

        #region INewsRepository Members
        public IList<NewsItem> GetNewsItemsByTagID(int TagID)
        {
            NewsTag tag = _context.NewsTags.Single(x => x.TagID == TagID);

            return tag.NewsItems.OrderByDescending(x => x.ModifiedDate).ToList();
        }

        public void UpdateNewsTagTotals(NewsTag tag)
        {
            throw new NotImplementedException();
        }

        public void InsertNewsTag(NewsTag tag)
        {
            throw new NotImplementedException();
        }

        #endregion


        public IEnumerable<NewsCategory> GetAllCategories()
        {
            return _context.NewsCategories;
        }


        public string GetDescription(int CategoryID, string strCulture)
        {
            NewsCategoryDescription desc = _context.NewsCategoryDescriptions.Single(d => d.CategoryID == CategoryID && d.LanguageID == strCulture);
            return desc.Description;
        }


        public bool HasCategory(NewsItem news, int CategoryID)
        {
            bool bResult = false;
            var categs = news.NewsCategories.ToList();

            foreach (NewsCategory categ in categs)
            {
                if (categ.CategoryID == CategoryID)
                    bResult = true;
            }

            return bResult;
        }

        public void AddCategory(int NewsID, int CategoryID)
        {
            NewsItem currentNews = GetNewsItemByID(NewsID);
            NewsCategory catg = GetCategoryByID(CategoryID);
            currentNews.NewsCategories.Add(catg);
            Save();
        }

        public void AddTag(int NewsID, int TagID)
        {
            NewsItem currentNews = GetNewsItemByID(NewsID);
            NewsTag catg = GetNewsTagByTagID(TagID);
            currentNews.NewsTags.Add(catg);
            Save();
        }
        public void Save()
        {
            _context.SaveChanges();
        }


        public NewsItem GetNewsItemByID(int iNewsID)
        {
            return _context.NewsItems.Single(news => news.NewsItemID == iNewsID);
        }

        public NewsCategory GetCategoryByID(int CategoryID)
        {
            return _context.NewsCategories.SingleOrDefault(x => x.CategoryID == CategoryID);
        }


        public void AddNewsItem(NewsItem news)
        {
            _context.NewsItems.Add(news);
            Save();
        }


        public void RemoveCategory(int NewsItemID, int CategoryID)
        {
            NewsItem currentNews = GetNewsItemByID(NewsItemID);
            NewsCategory catg = GetCategoryByID(CategoryID);

            currentNews.NewsCategories.Remove(catg);
            Save();
        }


        public IList<NewsItem> GetAllNewsItems()
        {
            return _context.NewsItems.ToList();
        }
        public IQueryable<NewsItem> GetAllActiveNewsItemsInCurrentCulture(int? CategoryID)
        {
            IQueryable<NewsItem> result;
            string currentCulture = Thread.CurrentThread.CurrentCulture.ToString();
            if (CategoryID != null)
                result = _context.NewsItems.Where(news => news.Active == true && news.LanguageID == currentCulture).Where(x => x.NewsCategories.Any(c => c.CategoryID == CategoryID));
            else
                result = _context.NewsItems.Where(news => news.Active == true && news.LanguageID == currentCulture);
            return result;
        }
        /*tags*/
        public virtual NewsTag GetNewsTagByTagID(int TagID)
        {
            return _context.NewsTags.Single(x => x.TagID == TagID);
        }

        public void Delete(NewsTag tag)
        {
            _context.NewsTags.Remove(tag);
            _context.SaveChanges();
        }

        public IList<NewsTag> GetAllNewsTags()
        {
            return _context.NewsTags.ToList();
        }

        public NewsTag GetNewsTagByName(string Name)
        {
            return _context.NewsTags.SingleOrDefault(x => x.Name == Name);
        }

        public void Insert(NewsTag tag)
        {
            _context.NewsTags.Add(tag);
            _context.SaveChanges();
        }

        public void Update(NewsTag tag)
        {
            _context.SaveChanges();
        }
        /*end-tags*/
    }
}
